﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Adapters;
using OboutInc.SlideMenu;
using OboutInc.SlideMenu.Designer;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web.Configuration;
using sonicERPinfo;
using sonicERPinterface;
using System.Xml;
using System.Xml.Serialization;



namespace sonicERPinterface.Charp
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string select = "select * from Cliente c inner join TipoTelefoneCliente_Info t on c.Id = t.idCliente";
                DataSet ds = new DataSet("cli");
                DataSet clie = new DataSet("clientes");
                DataTable cliente = new DataTable("cliente");
                cliente.Columns.Add("id", typeof(Int32));
                cliente.Columns.Add("Nome", typeof(String));

                DataTable telefone = new DataTable("telefone");
                telefone.Columns.Add("id", typeof(Int32));
                telefone.Columns.Add("Telefone", typeof(String));


                DataTable resultado = new DataTable("resultado");
                resultado.Columns.Add("id", typeof(Int32));
                resultado.Columns.Add("Nome", typeof(String));
                resultado.Columns.Add("Telefone", typeof(String));


                SqlConnection cn = new SqlConnection(WebConfigurationManager.ConnectionStrings["sonicerp"].ConnectionString);
                cn.Open();
                SqlDataAdapter sd = new SqlDataAdapter(select, cn);
                sd.Fill(ds);
                cn.Close();

                for (int i = 0; i < ds.Tables["Table"].Rows.Count; i++)
                {

                    DataRow dr = cliente.NewRow();
                    dr["id"] = ds.Tables["Table"].Rows[i]["id"].ToString();
                    dr["Nome"] = ds.Tables["Table"].Rows[i]["Nome"].ToString();
                    cliente.Rows.Add(dr);

                    DataRow dte = telefone.NewRow();
                    dte["id"] = ds.Tables["Table"].Rows[i]["idCliente"].ToString();
                    dte["Telefone"] = ds.Tables["Table"].Rows[i]["Telefone"].ToString();
                    telefone.Rows.Add(dte);


                }

                DataView dt = new DataView(cliente);
                cliente = dt.ToTable(true, "id", "Nome");
                DataView dtel = new DataView(telefone);
                telefone = dtel.ToTable(true,"id", "telefone");

                List<ClienteInfo> lstcli = new List<ClienteInfo>();
                ClienteInfoSerealize seralize = new ClienteInfoSerealize();
                foreach (DataRow item in cliente.Rows)
                {
                    ClienteInfo cli = new ClienteInfo();

                    cli.id = (Int32)item["id"];
                    cli.nome = (String)item["Nome"];

                    foreach (DataRow itemTel in telefone.Rows)
                    {
                        TelefoneInfo tele = new TelefoneInfo();
                        
                        if (item["id"].Equals(itemTel["id"]))
                        {
                        tele.tipoTelefone = (String)itemTel["telefone"];
                        cli.telefones.Add(tele);
                        }
                    }

                    lstcli.Add(cli);
                    seralize.Clientes = lstcli;
                }

                string xml = (String)new sonicERPinterface.Util().gerarxml(seralize);


            }
            //{
            //    SlideMenu1.AddParent("teste", "teste");
            //    SlideMenu1.AddParent("id1", "teste1");
            //    SlideMenu1.AddParent("ee", "teste1");

            //    SlideMenu1.AddChildAt("teste1", "teste", "filho", "WebForm1.aspx", null, null);
            //    int pai = SlideMenu1.Parent.Controls.Count;
            //}

        }
    }
}